package com.atguigu.guli.service.edu.service.impl;

import com.atguigu.guli.service.bean.eduBean.CrouseBean;
import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.CourseDescription;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.form.CourseListQueryAll;
import com.atguigu.guli.service.edu.entity.form.CoursePublishVo;
import com.atguigu.guli.service.edu.entity.form.CourseQuery;
import com.atguigu.guli.service.edu.entity.form.CourseQueryVo;
import com.atguigu.guli.service.edu.mapper.CourseDescriptionMapper;
import com.atguigu.guli.service.edu.mapper.CourseMapper;
import com.atguigu.guli.service.edu.mapper.TeacherMapper;
import com.atguigu.guli.service.edu.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.sql.Wrapper;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-06-23
 */
@Service
@Transactional
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    CourseMapper courseMapper;
    @Autowired
    CourseDescriptionMapper courseDescriptionMapper;

    @Override
    public String saveCourse(CoursePublishVo coursePublishVo) {
        System.out.println("coursePublishVo = " + coursePublishVo.getDescription());
        //应为表单中有两个数据库的表字段,所以要对两个表进行添加,
        Course course = new Course();
        CourseDescription courseDescription = new CourseDescription();
        //给course进行存值
        BeanUtils.copyProperties(coursePublishVo, course);
        courseMapper.insert(course);
        //获取新增的Id
        String courseId = course.getId();
        //把id赋值
        courseDescription.setId(courseId);
        courseDescription.setDescription(coursePublishVo.getDescription());
        courseDescriptionMapper.insert(courseDescription);

        return courseId;
    }

    @Override //分页查询
    public Page<CourseListQueryAll> QueryPageOtherFrom(Integer pageNum, Integer pageSize) {
        Page<CourseListQueryAll> page = new Page(pageNum, pageSize);
        //查询条件的拼接,,mybatis-plus会自动拼接查询条件跟分页limit
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.orderByAsc("t1.gmt_create");
        /*wrapper.like("t1.title", name);*/

        List<CourseListQueryAll> courseList = courseMapper.QueryPageOtherFrom(page,wrapper);
        page.setRecords(courseList);
        return page;
    }

    @Override
    public CourseListQueryAll getCourseById(String courseId) {
        QueryWrapper<CourseListQueryAll> wrapper = new QueryWrapper<>();
        wrapper.eq("t1.id", courseId);
        return courseMapper.getCourseById(wrapper);
    }

    @Override
    public List<Course> getCrouseQueryAll(CourseQuery courseQuery) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        // 根据上面进行排序
        String orderByColumn = courseQuery.getOrderByColumn();
        // 查询二级课程
        String subjectId = courseQuery.getSubjectId();
        //查询一级课程
        String subjectParentId = courseQuery.getSubjectParentId();
        // 降序还是升序
        Integer type = courseQuery.getType();
        //进行判断
        if(!StringUtils.isEmpty(subjectParentId)){
            wrapper.eq("subject_parent_id", subjectParentId);
        }
        if(!StringUtils.isEmpty(subjectId)){
            wrapper.eq("subject_id", subjectId);
        }
        if(!StringUtils.isEmpty(orderByColumn)){
            //如果不为空,就降序排序
            if(type!=null){
                wrapper.orderByDesc(orderByColumn);
            }else{
                //否则就升序
                wrapper.orderByAsc(orderByColumn);
            }
        }
        List<Course> courses = courseMapper.selectList(wrapper);
        return courses;
    }

    @Override
    public CourseQueryVo getCourseAllMcg(String courseId) {
        //查出信息
        CourseQueryVo courseAllMcg = courseMapper.getCourseAllMcg(courseId);
        //每次访问页面,游览数量+1
        courseAllMcg.setViewCount(courseAllMcg.getViewCount()+1);
        //向数据库修改
        UpdateWrapper<Course> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", courseId);
        wrapper.set("view_count", courseAllMcg.getViewCount());
        this.update(wrapper);
        return courseAllMcg;

        //return courseMapper.getCourseAllMcg(courseId);
    }

    @Autowired
    TeacherMapper teacherMapper;

    @Override
    public CrouseBean getCourseFromTrade(String courseId) {
        Course course = courseMapper.selectById(courseId);
        Teacher teacher = teacherMapper.selectById(course.getTeacherId());
        CrouseBean crouseBean = new CrouseBean();
        //讲查到的数据拷贝到crouseBean
        BeanUtils.copyProperties(course, crouseBean);

        crouseBean.setTeacherName(teacher.getName());
        return crouseBean;
    }


}
